package popup

import android.support.v7.widget.CardView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import app.AppContext
import com.hontech.kdxclient.R
import ext.log
import ext.randomString
import manager.*
import view.LoadImageView

class PayWindow : Runnable {
    companion object {
        var isShow = false
        val Instance: PayWindow by lazy { PayWindow() }
        private val ids = intArrayOf(
            R.id.id_pay_image_view_star_1,
            R.id.id_pay_image_view_star_2,
            R.id.id_pay_image_view_star_3,
            R.id.id_pay_image_view_star_4,
            R.id.id_pay_image_view_star_5
        )
        const val TIME_OUT = 120
    }

    private val view = LayoutInflater.from(AppContext).inflate(R.layout.popup_pay, null)
    private val iconImageView = view.findViewById<LoadImageView>(R.id.id_pay_image)
    private val stars = Array<ImageView>(5) { view.findViewById(ids[it]) }
    private val textViewPrice = view.findViewById<TextView>(R.id.id_pay_text_view_price)
    private val buttonCancel = view.findViewById<Button>(R.id.id_pay_button_cancel)
    private val cardView = view.findViewById<CardView>(R.id.id_pay_card_view)
    private val qrCodeImageView = view.findViewById<ImageView>(R.id.id_pay_qrcode_image_view)
    private val imageAlipay = view.findViewById<ImageView>(R.id.id_popup_pay_ic_alipay)
    private val imageWechat = view.findViewById<ImageView>(R.id.id_popup_pay_ic_wechat)
    private var timerOutCount = TIME_OUT

    private val window =
        PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true)

    init {
        buttonCancel.setOnClickListener(::onClickCancel)
      //  view.setOnClickListener(this)
        cardView.setOnClickListener { }
        window.animationStyle = R.style.PayTheme
        imageAlipay.visibility = View.VISIBLE
        imageWechat.visibility = View.VISIBLE
    }

    override fun run() {
        buttonCancel.text = "取消付款($timerOutCount)"
        timerOutCount--
        if (timerOutCount <= 0) {
            PayManager.refundAll(PayManager.CurrentOrder)
            dismiss()
            return
        }
        TaskManager.UiHandler.postDelayed(this, 1000)
    }

//    override fun onClick(v: View?) {
//        dismiss()
//        PayManager.refundAll(PayManager.CurrentOrder)
//    }

    private fun onClickCancel(view: View) {
        dismiss()
        PayManager.refundAll(PayManager.CurrentOrder)
    }

    private fun initUi() {
        val info = WaresManager.selectInfo
        val order = System.currentTimeMillis().toString() + randomString(3) + AppManager.MacAddressTopic

        val url = "${AppManager.PayWebUrl}?" +
                "MacAddress=${AppManager.MacAddress}&" +
                "GoodsId=${info.id}&" +
                "Order=$order&" +
                "Price=${info.price}&" +
                "Discount=10.0"

        PayManager.CurrentOrder = order
        log(url, "二合一二维码")
        iconImageView.setImageWithUrl(info.maxImageUrl)
        timerOutCount = TIME_OUT
        TaskManager.UiHandler.post(this)

        val bitmap = BarcodeManager.create(url)
        qrCodeImageView.setImageBitmap(bitmap)

        val star = info.starValue
        for (i in 0 until 5) {
            if (i < star) {
                stars[i].visibility = View.VISIBLE
            } else {
                stars[i].visibility = View.GONE
            }
        }
        textViewPrice.text = "${info.name}:¥${info.price}"
    }

    fun dismiss() {
        if (!isShow) {
            return
        }
        TaskManager.UiHandler.removeCallbacks(this)
        window.dismiss()
        AppManager.isBusy = false
        isShow = false
    }

    fun show(view: View) {
        if (isShow) {
            return
        }
        isShow = true
        AppManager.isBusy = true
        window.showAtLocation(view, Gravity.CENTER, 0, 0)
        initUi()
    }
}